// Random.cpp: implementation of the CRandom class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "../Predefine.h"
#include "Random.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
#ifdef __SYMBIAN32__
#elif defined _BREWVERSION
	#include "AEEStdLib.h"
//	#include "../Brew/Predefine.h"
	#include "../Brew/MyAEEApplet.h"
#elif defined _WIPIVERSION
	#include "HQM3DPredefine.h"
	#include "MemoryAlloc.h"
	CRandom * CRandom::m_pRandom = NULL;
#elif defined IPHONEOS
	#include "Commons.h"
	CRandom * CRandom::m_pRandom = NULL;
#else
	CRandom * CRandom::m_pRandom = NULL;
#endif


CRandom::CRandom(unsigned int in_Seed) :
    m_Seed(in_Seed)
{
#ifdef __SYMBIAN32__
	_TlsStruct * pStruct = (_TlsStruct*)Dll::Tls();
	pStruct->member[2] = this;
	Dll::SetTls((TAny*)pStruct);
#elif defined _BREWVERSION
#else
	m_pRandom = this;
#endif
}

CRandom::CRandom()
{
#ifdef __SYMBIAN32__
	_TlsStruct * pStruct = (_TlsStruct*)Dll::Tls();
	pStruct->member[2] = this;
	Dll::SetTls((TAny*)pStruct);
#elif defined _BREWVERSION
#else
	m_pRandom = this;
#endif
   m_Seed = 0;
}


CRandom::~CRandom()
{
#ifdef __SYMBIAN32__
	_TlsStruct * pStruct = (_TlsStruct*)Dll::Tls();
	pStruct->member[2] = NULL;
	Dll::SetTls((TAny*)pStruct);
#elif defined _BREWVERSION
#else
	m_pRandom = NULL;
#endif
}

int CRandom::Random(int nMin,int nMax)
{
	CRandom * pRandom = GetRandom();
	return pRandom->GetNumber(nMin,nMax+1);
}

int CRandom::Random(int nMax)
{
	CRandom * pRandom = GetRandom();
	return pRandom->GetNumber(nMax+1);
}

CRandom * CRandom::GetRandom()
{
	CRandom * pManager;
#ifdef __SYMBIAN32__
	_TlsStruct * pStruct = (_TlsStruct*)Dll::Tls();
	pManager = (CRandom*)pStruct->member[2];
#elif defined _BREWVERSION
	pManager = CMyAEEApplet::GetRandom();
#else
	pManager = m_pRandom;
#endif
	return pManager;
}
